package com.antoco.controlcenter.model.video.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.antoco.controlcenter.R;
import com.antoco.controlcenter.base.BaseFragment;
import com.antoco.controlcenter.comm.RobotHandler;
import com.antoco.controlcenter.comm.message.NciMessage;
import com.antoco.controlcenter.entity.ButtonEntity;
import com.antoco.controlcenter.entity.GroupEntity;
import com.antoco.controlcenter.entity.SliderEntity;
import com.antoco.controlcenter.utils.AddViewUtils;
import com.antoco.controlcenter.utils.BytesUtils;
import com.antoco.controlcenter.utils.StringUtils;
import com.antoco.controlcenter.view.StrokeTextView;

import java.util.List;

public class ContorFragmet extends BaseFragment implements SeekBar.OnSeekBarChangeListener, Button.OnClickListener {
    RobotHandler robothandler;
    List<GroupEntity> groupList;
    List<SliderEntity> sliderList;

    public ContorFragmet(RobotHandler robothandler, List<GroupEntity> groupList, List<SliderEntity> sliderList) {
        this.robothandler = robothandler;
        this.groupList = groupList;
        this.sliderList = sliderList;

    }

    @Override
    protected View getContentView() {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.auto_view, null);
        LinearLayout addLl = view.findViewById(R.id.ll);
        addGroup(addLl);
        addSeekbar(addLl);
        return addLl;
    }

    @Override
    protected void initBundleData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    private void addGroup(LinearLayout addLl) {
        if (null != groupList && !groupList.isEmpty()) {
            groupList.forEach(groupEntity -> {
                if (!StringUtils.isEmpty(groupEntity.getName())) {
                    StrokeTextView textView = new StrokeTextView(mContext);
                    String textViewName = groupEntity.getName();
                    textView.setTextColor(mContext.getResources().getColor(R.color.white, null));
                    textView.setText(textViewName);
                    addLl.addView(textView);
                }
                // 组容器
                LinearLayout linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                List<ButtonEntity> buttonEntities = groupEntity.getButtonList();
                if (null != buttonEntities && !buttonEntities.isEmpty()) {
                    buttonEntities.forEach(buttonEntity -> {
                        AddViewUtils.addButtomView(mContext, buttonEntity, linearLayout, this);
                    });
                }
                addLl.addView(linearLayout);
            });
        }

    }

    private void addSeekbar(LinearLayout addLl) {
        if (null != sliderList && !sliderList.isEmpty()) {
            sliderList.forEach(sliderEntity -> {
                AddViewUtils.addSeekBarView(mContext, addLl, sliderEntity, this);
            });
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        //判断改变的是否是指定的滑动条。预防当前Activity对应的xml布局中有多个滑动条，如果只有一个滑动条，则不必判断
        String tag = (String) seekBar.getTag();
        if (tag != null) {
            String[] cmdList = tag.split("\\|");
            if (cmdList.length > 0) {
                switch (cmdList[0]) {
                    case "property":
                        if (cmdList.length > 1) {
                            robothandler.propertyManager.setPropertieNumberValue(cmdList[1], (double) progress);
                        }
                        break;
                    case "nci":
                        if (cmdList.length > 1) {
                            String str = cmdList[1].replaceAll(" ", "");
                            byte[] nciBuffer = BytesUtils.hexStringToByteArray(str);
                            NciMessage nci = new NciMessage(nciBuffer);
                            if (cmdList.length > 2) {
                                if (cmdList[2].equals("value")) {
                                    nci.SetData(progress);
                                }
                            }
                            robothandler.addNci(nci);
                        }
                        break;
                }
            }
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onClick(View view) {
        String tag = (String) view.getTag();
        if (tag != null) {
            String[] cmdList = tag.split("\\|");
            if (cmdList.length > 0) {
                switch (cmdList[0]) {
                    case "property":
                        if (cmdList.length > 1) {
//                                    robot.propertyManager.setPropertieNumberValue(cmdList[1],(double)progress);
                        }
                        break;
                    case "nci":
                        if (cmdList.length > 1) {
                            String str = cmdList[1].replaceAll(" ", "");
                            byte[] nciBuffer = BytesUtils.hexStringToByteArray(str);
                            NciMessage nci = new NciMessage(nciBuffer);
                            if (cmdList.length > 2) {
                                if (cmdList[2].equals("value")) {
//                                            nci.SetData(progress);
                                }
                            }
                            robothandler.addNci(nci);
                        }
                        break;
                }
            }
        }
    }

}
